from functools import wraps

from flask import Flask, abort, Blueprint
from flask.sansio.scaffold import setupmethod
import typing as t

from src.baseapp.requestVerify.BaseRequestVerify import RequestVerifier

from http import HTTPStatus

class CustomBlueprint(Blueprint):

    @setupmethod
    def route(self, rule: str, header_verify=None, body_verify=None, **options: t.Any):
        """
        重新封装Blueprint默认的route方法，增加header和body的校验
        """
        def decorator(f):
            @wraps(f)
            def wapper(*args, **kwargs):
                request_obj = RequestVerifier(header_verify, body_verify).verify()
                if not isinstance(request_obj, RequestVerifier.Result):
                    abort(HTTPStatus.BAD_REQUEST, request_obj)
                return f(request_obj, *args, **kwargs)
            endpoint = options.pop("endpoint", None)
            self.add_url_rule(rule, endpoint, wapper, **options)
            return f
        return decorator


app = Flask("EasyTest")
app.url_map.strict_slashes = False
